package com.kizuki.algorithm.leetcode;

/**
 * 给定一个二叉树，判断它是否是高度平衡的二叉树。
 *
 * https://leetcode-cn.com/problems/balanced-binary-tree/
 */
public class 平衡二叉树 {

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null) {
                return true;
            }
            return Math.abs(height(root.left) - height(root.right)) < 2 && isBalanced(root.left) && isBalanced(root.right);
        }

        private int height(TreeNode node) {
            if (node == null) {
                return 0;
            }
            return Math.max(height(node.left), height(node.right)) + 1;
        }
    }
}
